<?php
class Uploader {
	
	/**
	 * Path to upload directory
	 *
	 * @var string
	 */
	protected $_uploadDir;	
	
	/**
	 * Path to thumbnails directory
	 *
	 * @var string
	 */
	protected $_thumbDir;
	
	/**
	 * Set up upload directory
	 *
	 * @param string $uploadDir
	 */
	public function setUploadDir($uploadDir) {
		/*if(!is_dir($uploadDir)) {
			d("here", false);
			return;
		}*/
		$this->_uploadDir = $uploadDir;
	}
	
	/**
	 * Upload file to the server
	 *
	 * @return unknown
	 */
	public function upload() {
		$uplDir = BASE_DIR. '/' . $this->_uploadDir;
		if(isset($_FILES['origUserPic'])) {
			$fileName = $this->_makeValidUploadedName() . '.jpg';
			if(!move_uploaded_file($_FILES['origUserPic']['tmp_name'], $uplDir . '/' . $fileName)) {
				return;
			}
			$shortNewsPicFile = $this->_uploadDir . '/' . $fileName;
			return $shortNewsPicFile;
		}
		return 0;
	}
	
	protected function _makeValidUploadedName() {
		return md5('foto' . $_FILES['origUserPic']['name'] . rand(1, 10000));
	}
}